package com.zbl.zblproject.erp.entity;



import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Optional;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2019/1/4
 */
public class ErpTableEntity {

    // index 序号
    // supplierName 供应商名称
    // cgddNumber CGDD订单商品总数量
    // realIntoNumber 入库商品总数量
    // differenceNumber 差异数量
    // cgddPrice CGDD商品总价
    // intoPrice 入库商品总价
    // differencePrice 差异商品总价
    // finishPrice  供应商财务金额

    private DecimalFormat formater = new DecimalFormat("###,##0.00");

    private int id;

    /**
     * 名称
     */
    private String name;

    /**
     * 订单商品总数量
     */
    private Integer orderNumber;

    /**
     * 入库商品总数量
     */
    private Integer intoNumber;

    /**
     * 退款商品总数量
     */
    private Integer refundNumber;


    /**
     * 商品总价
     */
    private BigDecimal goodsPrice;

    /**
     * 入库总价
     */
    private BigDecimal intoPrice;

    /**
     * 财务审核完成订单
     */
    private BigDecimal finishPrice;

    /**
     * 退款总金额
     */
    private BigDecimal refundPrice;

    private List<ErpTableEntity> clients;

    private Integer differenceNumber;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getOrderNumber() {
        return Optional.ofNullable(orderNumber).orElse(0);
    }

    public void setOrderNumber(Integer orderNumber) {
        this.orderNumber = orderNumber;
    }

    public Integer getIntoNumber() {
        return Optional.ofNullable(intoNumber).orElse(0);
    }

    public void setIntoNumber(Integer intoNumber) {
        this.intoNumber = intoNumber;
    }

    public Integer getDifferenceNumber() {
       return  getOrderNumber() - getIntoNumber() - getRefundNumber();
    }

    public BigDecimal getGoodsPrice() {
        return defaultZero(goodsPrice);
    }

    public String getGoodsPriceFormat(){
        return formater.format(getGoodsPrice());
    }

    public void setGoodsPrice(BigDecimal goodsPrice) {
        this.goodsPrice = goodsPrice;
    }

    public BigDecimal getIntoPrice() {
        return defaultZero(intoPrice);
    }

    public String getIntoPriceFormat(){
        return formater.format(getIntoPrice());
    }

    public void setIntoPrice(BigDecimal intoPrice) {
        this.intoPrice = intoPrice;
    }

    public BigDecimal getDifferencePrice() {
        return getGoodsPrice().subtract(getIntoPrice()).subtract(getRefundPrice());
    }

    public String getDifferencePriceFormat(){
        return formater.format(getDifferencePrice());
    }


    public BigDecimal getFinishPrice() {
        return defaultZero(finishPrice);
    }

    public String getFinishPriceFormat(){
        return formater.format(getFinishPrice());
    }

    public void setFinishPrice(BigDecimal finishPrice) {
        this.finishPrice = finishPrice;
    }

    private BigDecimal defaultZero(BigDecimal val){
        return Optional.ofNullable(val).orElse(BigDecimal.ZERO);
    }

    public List<ErpTableEntity> getClients() {
        return clients;
    }

    public void setClients(List<ErpTableEntity> clients) {
        this.clients = clients;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Integer getRefundNumber() {
        return Optional.ofNullable(refundNumber).orElse(0);
    }

    public void setRefundNumber(Integer refundNumber) {
        this.refundNumber = refundNumber;
    }

    public BigDecimal getRefundPrice() {
        return defaultZero(refundPrice);
    }

    public String getRefundPriceFormat(){
        return formater.format(getRefundPrice());
    }

    public void setDifferenceNumber(Integer differenceNumber) {
        this.differenceNumber = differenceNumber;
    }

    public void setRefundPrice(BigDecimal refundPrice) {
        this.refundPrice = refundPrice;
    }

    //合计订单数量
    public Integer getAllOrderNum(){
        if(clients == null || clients.isEmpty()){
            return 0;
        }
        return clients.stream().mapToInt(ErpTableEntity::getOrderNumber).sum();
    }

    //合计出库数量
    public Integer getAllRefundNumber(){
        if(clients == null || clients.isEmpty()){
            return 0;
        }
        return clients.stream().mapToInt(ErpTableEntity::getRefundNumber).sum();
    }

    //合计差异数量
    public Integer getAllDiffNum(){
        if(clients == null || clients.isEmpty()){
            return 0;
        }
        return clients.stream().mapToInt(ErpTableEntity::getDifferenceNumber).sum();
    }

    //合计订单金额
    public String getAllOrderPrice(){
        if(clients == null || clients.isEmpty()){
            return "0.0";
        }
        BigDecimal price = BigDecimal.valueOf(clients.stream().mapToDouble(x->{
            return x.getGoodsPrice().doubleValue();
        }).sum());
        return formater.format(price);
    }

    //合计出库金额
    public String getAllRefundPrice(){
        if(clients == null || clients.isEmpty()){
            return "0.0";
        }
        BigDecimal price = BigDecimal.valueOf(clients.stream().mapToDouble(x->{
            return x.getRefundPrice().doubleValue();
        }).sum());
        return formater.format(price);
    }

    //合计差异金额
    public String getAllDiffPrice(){
        if(clients == null || clients.isEmpty()){
            return "0.0";
        }
        BigDecimal price = BigDecimal.valueOf(clients.stream().mapToDouble(x->{
            return x.getDifferencePrice().doubleValue();
        }).sum());
        return formater.format(price);
    }



}
